//index.js
var util = require('../../utils/util.js')
//获取应用实例
const app = getApp()

Page({
  data: {
    list: [],
    total: 0,
    loadingMore: false,
    noMoreData: false,
    searchKeyword:""
  },
  currentPageNumber: 1,
  //初始化
  onLoad() {
 
  },
  onShow() {
    this.onPullDownRefresh();
  },
  //下拉刷新
  onPullDownRefresh(){
    var that = this;
    this.currentPageNumber = 1;
    this.setData({
      noMoreData: false,
      noData: false
    });
    var keyword = this.data.searchKeyword
   
    var info = {
      page: 1,
      num: 7,
      keyword: keyword
    }
    var cate_url = '/answer/index/index?num=7&page=' + this.currentPageNumber;
    util.commonAjax(cate_url, 1, info)
      .then(function (data) {
        var c_list = data.data.data
        if (c_list.length > 0) {
          that.currentPageNumber++;
          that.setData({
            list: c_list
          })
        } else {
          that.setData({
            noMoreData: true
          });
        }
        wx.stopPullDownRefresh();
      })
  },
  //上拉刷新
  onReachBottom() {
    this.pullUpLoad();
  },
  pullUpLoad() {
    var that = this;
    if (this.data.loadingMore || this.data.noMoreData) return;
    this.setData({
      loadingMore: true
    });
    wx.showNavigationBarLoading();
    var keyword = this.data.searchKeyword
    var info = {
      page: that.currentPageNumber,
      num: 7,
      keyword: keyword
    }
    var cate_url = '/answer/index/index?num=7&page=' + this.currentPageNumber;
    util.commonAjax(cate_url, 1, info)
      .then(function (data) {
        var c_list = data.data.data
        if (c_list.length > 0) {
          that.currentPageNumber++;
          var y_list = that.data.list;
          c_list = y_list.concat(c_list)
          console.log(c_list)
          that.setData({
            list: c_list
          })
        } else {
          that.setData({
            noMoreData: true
          });
        }
        that.setData({
          loadingMore: false
        });
        wx.hideNavigationBarLoading();
      })
  },
  

  showSearchInput() {
    this.setData({
      searchInputShowed: true,
      searchingResult: false
    });
  },
  hideSearchInput() {
    this.setData({
      searchInputVal: "",
      searchKeyword: "",
      searchInputShowed: false,
      searchingResult: false
    });
    this.onPullDownRefresh();
  },
  clearSearchInput() {
    this.setData({
      searchInputVal: "",
      searchKeyword: "",
      searchingResult: false
    });
    this.onPullDownRefresh();
  },
  searchInputTyping(e) {
    this.setData({
      searchInputVal: e.detail.value,
      searchKeyword: "",
      searchingResult: false
    });
  },
  searchSubmit() {
    this.setData({
      searchingResult: true,
      searchKeyword: this.data.searchInputVal
    });
    this.onPullDownRefresh();
  },
  start_test(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/test/test?id=' + id
    });

  },
  see_score(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/score/score?id=' + id
    });

  },

})
